package com.ltkj.order.domain;

import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class OrderDelayed implements Delayed {

    /**
     * 延迟时间
     */
    private final Long time;
    /**
     * 订单编号
     */
    public Long orderId;

    public OrderDelayed(Long orderId, long time, TimeUnit unit) {
        this.orderId = orderId;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        OrderDelayed orderDelayed = (OrderDelayed) o;
        long diff = this.time - orderDelayed.time;
        if (diff <= 0) {
            return -1;
        } else {
            return 1;
        }
    }
}
